package com.kpx.app.utils;

import android.content.Context;
import android.text.TextUtils;
import android.widget.Toast;

import com.kpx.app.bean.ShareContent;
import com.kpx.app.R;
import com.kpx.app.Application;
import com.kpx.app.utils.uiutils.ToastUtil;

import cn.sharesdk.onekeyshare.OnekeyShare;

/**
 * 作者： Ryan
 * 描述：
 * 创建时间：2015/5/19 10:31.
 */
public class ShareSDKUtils {
    private static ShareSDKUtils shareSDKUtils;

    /**
     * 获取分享内容
     */
    private final int GET_SHARE_CONTENT = 1;

    private OnShareListener mListener;

    private ShareSDKUtils() {
    }

    public static ShareSDKUtils getInstance() {
        if (shareSDKUtils == null) {
            synchronized (ShareSDKUtils.class) {
                shareSDKUtils = new ShareSDKUtils();
            }
        }
        return shareSDKUtils;
    }

    /**
     * 获取分享的内容
     *
     * @param id    第三方id(用户分享 =用户ID，活动分享=活动id，券分享=券id)
     * @param stype 0用户分享 1活动分享 2券分享
     */
    public void startShare(String id, int stype, OnShareListener listener) {
//        mListener = listener;
//        HttpUtils.getHttpUtils().getShareContent(GET_SHARE_CONTENT, id, stype, this);
    }

    public void setListener(OnShareListener listener) {
        mListener = listener;
    }

//    @Override
//    public void onNetworkResponse(int requestFlag, NetworkResult networkResult) {
//        int resultCode = networkResult.getCode();
//        if (Constants.CODE_SUCCESS != resultCode) {
//            String msg = networkResult.getMsg();
//            if (TextUtils.isEmpty(msg)) {
//                msg = Constants.ERROR_MAP1.get(resultCode);
//            }
//            if (TextUtils.isEmpty(msg)) {
//                msg = Application.instance.getString(R.string.server_busy_retry_later);
//            }
//            ToastUtil.getInstance().showToast(Application.instance, msg);
//        }
//        if (resultCode == Constants.CODE_SUCCESS) {
//            ShareContent shareContent = networkResult.getObject(networkResult.getObjectString("shareMes"), ShareContent.class);
//            share(shareContent, Application.instance);
//            if (null != mListener) {
//                mListener.onShareSuccessListener();
//            }
//        } else {
//            ToastUtil.getInstance().showToast(Application.instance, Constants.ERROR_MAP.get(resultCode), Toast.LENGTH_SHORT);
//            if (null != mListener) {
//                mListener.onShareFailListener();
//            }
//        }
//    }

    /**
     * 弹出分享提示
     *
     * @param shareContent
     * @param context
     */
    public void share(ShareContent shareContent, Context context) {
        if (null != shareContent && null != context) {
            OnekeyShare oks = new OnekeyShare();
            if (null != oks) {
                oks.setTitle(shareContent.getTitle());
                oks.setText(shareContent.getContent());
                oks.setSite(shareContent.getTargetUrl());
                oks.setTitleUrl(shareContent.getTargetUrl());
                oks.setUrl(shareContent.getTargetUrl());
                oks.setSiteUrl(shareContent.getTargetUrl());
                oks.setSite(Application.instance.getString(R.string.app_name));
//                oks.setTitleUrl("http://www.ppsh.co/");
                oks.setSilent(false);
                oks.setText(shareContent.getContent());
                oks.setImageUrl(shareContent.getImgs());
                oks.setUrl(shareContent.getTargetUrl());
                oks.show(context);
            } else {
                ToastUtil.getInstance().showToast(context, R.string.share_msg_fail, Toast.LENGTH_SHORT);
            }
        } else {
            ToastUtil.getInstance().showToast(context, R.string.get_share_msg_fail, Toast.LENGTH_SHORT);
        }
    }

    public void pureShare(ShareContent shareContent, Context context) {
        if (null != shareContent && null != context) {
            OnekeyShare oks = new OnekeyShare();
            oks.setTitle(shareContent.getTitle());
            oks.setText(shareContent.getContent());
            oks.setSite(shareContent.getTargetUrl());
            oks.setTitleUrl(shareContent.getTargetUrl());
            oks.setUrl(shareContent.getTargetUrl());
            oks.setSiteUrl(shareContent.getTargetUrl());
            oks.setSite(Application.instance.getString(R.string.app_name));
//                oks.setTitleUrl("http://www.ppsh.co/");
            oks.setSilent(false);
            oks.setText(shareContent.getContent());
            if(!TextUtils.isEmpty(shareContent.getImgs())) {
                oks.setImageUrl(shareContent.getImgs());
            }
            oks.setUrl(shareContent.getTargetUrl());
            oks.show(context);
        } else {
            ToastUtil.getInstance().showToast(context, R.string.get_share_msg_fail, Toast.LENGTH_SHORT);
        }
    }

    public interface OnShareListener {
        /**
         * 分享成功回调
         */
        void onShareSuccessListener();

        /**
         * 分享失败回调
         */
        void onShareFailListener();
    }
}
